package lib.quasar.widget.text;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description:
 * created by kalu on 2018/7/6 12:01
 */
@SuppressLint("AppCompatCustomView")
public class MultTextView extends TextView {

    private boolean textBold = false;

    private final int[][] mState = new int[][]{
            new int[]{android.R.attr.state_pressed},
            new int[]{android.R.attr.state_enabled}
    };

    private final StateListDrawable mBackgroundDrawable = new StateListDrawable();
    private final GradientDrawable mNormalDrawable = new GradientDrawable();
    private final GradientDrawable mPressDrawable = new GradientDrawable();

    private float iconWidthLeft = 0f;
    private float iconHeightLeft = 0f;

    private float iconWidthRight = 0f;
    private float iconHeightRight = 0f;

    private float lineSize = 0f;
    private float stockSize = 0f;
    private int lineColor = Color.TRANSPARENT;

    private boolean localtionTop = false;
    private boolean localtionBottom = false;
    private boolean localtionLeft = false;
    private boolean localtionRight = false;

    private float paddingTop = 0;
    private float paddingBottom = 0;
    private float paddingLeft = 0;
    private float paddingRight = 0;

    public MultTextView(Context context) {
        this(context, null);
    }

    public MultTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);

        TypedArray array = null;
        try {
            array = context.obtainStyledAttributes(attrs, R.styleable.MultTextView);

            iconWidthLeft = array.getDimension(R.styleable.MultTextView_mtv_icon_left_width, iconWidthLeft);
            iconHeightLeft = array.getDimension(R.styleable.MultTextView_mtv_icon_left_height, iconHeightLeft);

            iconWidthRight = array.getDimension(R.styleable.MultTextView_mtv_icon_right_width, iconWidthRight);
            iconHeightRight = array.getDimension(R.styleable.MultTextView_mtv_icon_right_height, iconHeightRight);
            textBold = array.getBoolean(R.styleable.MultTextView_mtv_text_bold, textBold);

            final float radius = array.getDimension(R.styleable.MultTextView_mtv_bg_radius, 0);
            stockSize = array.getDimension(R.styleable.MultTextView_mtv_stock_size, 0);

            final int color1 = array.getColor(R.styleable.MultTextView_mtv_bg_color_normal, Color.TRANSPARENT);
            final int color2 = array.getColor(R.styleable.MultTextView_mtv_stock_color_normal, Color.TRANSPARENT);
            mNormalDrawable.setColor(color1);
            mNormalDrawable.setCornerRadius(radius);
            mNormalDrawable.setStroke((int) stockSize, color2);

            final int color3 = array.getColor(R.styleable.MultTextView_mtv_bg_color_press, Color.TRANSPARENT);
            final int color4 = array.getColor(R.styleable.MultTextView_mtv_stock_color_press, Color.TRANSPARENT);
            mPressDrawable.setColor(color3);
            mPressDrawable.setCornerRadius(radius);
            mPressDrawable.setStroke((int) stockSize, color4);

            lineSize = array.getDimension(R.styleable.MultTextView_mtv_line_size, lineSize);
            lineColor = array.getColor(R.styleable.MultTextView_mtv_line_color, lineColor);

            localtionTop = array.getBoolean(R.styleable.MultTextView_mtv_location_top, false);

            localtionTop = array.getBoolean(R.styleable.MultTextView_mtv_location_top, false);
            localtionBottom = array.getBoolean(R.styleable.MultTextView_mtv_location_bottom, false);
            localtionLeft = array.getBoolean(R.styleable.MultTextView_mtv_location_left, false);
            localtionRight = array.getBoolean(R.styleable.MultTextView_mtv_location_right, false);
            paddingTop = array.getDimension(R.styleable.MultTextView_mtv_padding_top, 0);
            paddingBottom = array.getDimension(R.styleable.MultTextView_mtv_padding_bottom, 0);
            paddingLeft = array.getDimension(R.styleable.MultTextView_mtv_padding_left, 0);
            paddingRight = array.getDimension(R.styleable.MultTextView_mtv_padding_right, 0);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 左,上,右,下
        final Drawable[] drawables = getCompoundDrawables();
        if (null != drawables[0] && iconWidthLeft != 0 && iconHeightLeft != 0f) {
            final Drawable drawable = drawables[0];
            final Rect bounds = drawable.getBounds();
            if (bounds.width() != 0 && bounds.height() != 0) {
                final int newLeft = bounds.left;
                final int newTop = (int) (bounds.height() * 0.5f - iconHeightLeft * 0.5f);
                final int newBottom = (int) (bounds.height() * 0.5f + iconHeightLeft * 0.5f);
                final int newRight = (int) (newLeft + iconWidthLeft);
                drawable.setBounds(newLeft, newTop, newRight, newBottom);
            }
        }
        if (null != drawables[2] && iconWidthRight != 0 && iconHeightRight != 0f) {
            final Drawable drawable = drawables[2];
            final Rect bounds = drawable.getBounds();
            if (bounds.width() != 0 && bounds.height() != 0) {
                final int newLeft = bounds.left;
                final int newTop = (int) (bounds.height() * 0.5f - iconHeightRight * 0.5f);
                final int newBottom = (int) (bounds.height() * 0.5f + iconHeightRight * 0.5f);
                final int newRight = (int) (newLeft + iconWidthRight);
                drawable.setBounds(newLeft, newTop, newRight, newBottom);
            }
        }
        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    }

    @Override
    public void draw(Canvas canvas) {

        int left = localtionLeft ? (int) lineSize : 0;
        int top = localtionTop ? (int) lineSize : 0;
        int right = getWidth() - (localtionRight ? (int) lineSize : 0);
        int bottom = getHeight() - (localtionBottom ? (int) lineSize : 0);
        mPressDrawable.setBounds(left, top, right, bottom);
        mNormalDrawable.setBounds(left, top, right, bottom);
        mBackgroundDrawable.addState(mState[0], mPressDrawable);
        mBackgroundDrawable.addState(mState[1], mNormalDrawable);
//         mBackgroundDrawable.setEnterFadeDuration(50);
//         mBackgroundDrawable.setExitFadeDuration(50);
        setBackgroundDrawable(mBackgroundDrawable);

        // step3
        if (localtionTop) {
            getPaint().setAntiAlias(true);
            getPaint().setStrokeCap(Paint.Cap.ROUND);
            getPaint().setColor(lineColor);
            canvas.drawRect(paddingLeft, 0, getWidth() - paddingRight, lineSize, getPaint());
        }
        if (localtionBottom) {
            getPaint().setAntiAlias(true);
            getPaint().setStrokeCap(Paint.Cap.ROUND);
            getPaint().setColor(lineColor);
            canvas.drawRect(paddingLeft, getHeight() - lineSize, getWidth() - paddingRight, getHeight(), getPaint());
        }
        if (localtionLeft) {
            int height = getHeight();
            getPaint().setAntiAlias(true);
            getPaint().setStrokeCap(Paint.Cap.ROUND);
            getPaint().setColor(lineColor);
            canvas.drawRect(paddingLeft, paddingTop, lineSize, height - paddingBottom, getPaint());
        }
        if (localtionRight) {
            int height = getHeight();
            int width = getWidth();
            getPaint().setAntiAlias(true);
            getPaint().setStrokeCap(Paint.Cap.ROUND);
            getPaint().setColor(lineColor);
            canvas.drawRect(width - paddingRight - lineSize, paddingTop, width - paddingRight, height - paddingBottom, getPaint());
        }

        // step2
        getPaint().setFakeBoldText(textBold);
        super.draw(canvas);
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(true);
    }

    /**********************************************************************************************/

    public final void setNormalBackgroundColor(int color) {
        mNormalDrawable.setColor(color);
    }

    public final void setPressBackgroundColor(int color) {
        mPressDrawable.setColor(color);
    }

    public final void setRadius(int radius) {
        mNormalDrawable.setCornerRadius(radius);
        mPressDrawable.setCornerRadius(radius);
    }
}
